import useStore from '@/stores'

/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path)
}

/**
 * 按钮权限验证
 * @returns {boolean}
 * @param el
 * @param binding
 */
export const checkPermission = (el, binding) => {
  // && value instanceof Array && value.length > 0
  if (binding.value) {
    // 用户的各种权限
    const { userStore } = useStore()
    const { info } = userStore
    // 按钮权限 见mock/user.js
    const points = info.roles.points || []
    if (!points.includes(binding.value)) {
      el.style.display = 'none'
    }
  } else {
    console.error(`need points! Like v-permission="'admin"`)
    return false
  }
}

/**
 * 手机号验证
 * @param rule
 * @param value
 * @returns {Promise<never>|Promise<void>}
 */
export const phoneRegExp = (rule, value) => {
  // 手机号
  const reg = /^((13|14|15|16|17|18|19)[0-9]{1}\d{8})$/
  // 座机号——带区号
  const reg2 = /^[0][0-9]{2,3}-[0-9]{5,10}$/
  if (value === undefined || value === '' || value === null) {
    return Promise.reject('手机号不能为空')
  } else if (!reg.test(value) && !reg2.test(value)) {
    return Promise.reject('手机号格式错误')
  } else {
    return Promise.resolve()
  }
}
/**
 * 银行卡号验证
 * @param rule
 * @param value
 * @returns {Promise<never>|Promise<void>}
 */
export const bankAccountRegExp = (rule, value) => {
  const reg = /^\d{9,30}$/
  if (value === undefined || value === '' || value === null) {
    return Promise.reject('银行卡不能为空')
  } else if (!reg.test(value)) {
    return Promise.reject('银行卡格式错误')
  } else {
    return Promise.resolve()
  }
}
/**
 * 身份证号验证
 * @param rule
 * @param value
 * @returns {Promise<never>|Promise<void>}
 */
export const idCardRegExp = (rule, value) => {
  const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)$/
  if (value === '' || value === null) {
    return Promise.reject('身份证号不能为空')
  } else if (!reg.test(value)) {
    return Promise.reject('身份证号格式错误')
  } else {
    return Promise.resolve()
  }
}
